import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;

public class ReflectAnnotation {
    @Retention(RetentionPolicy.RUNTIME)
    public  @interface MyAnnotation {
        String[] value() default "unknown";
    }

    public class Operator{
        @MyAnnotation(value ={"aaa","cc","ee"})
        public void HasValue(){
            System.out.printf("HasValue");
        }

        @MyAnnotation
        public void NoneValue(){
            System.out.printf("NoneValue");
        }
    }

    public class ReflectAnnotationTest  {
        public void Test ()throws NoSuchMethodException {
            Class c=Operator.class;
            Method[] methods= c.getMethods();
            for (Method method:methods) {
                iteratorAnnotations(method);
            }
        }

        public void iteratorAnnotations(Method method) {

            // 判断 somebody() 方法是否包含MyAnnotation注解
            if(method.isAnnotationPresent(MyAnnotation.class)){
                // 获取该方法的MyAnnotation注解实例
                MyAnnotation myAnnotation = method.getAnnotation(MyAnnotation.class);
                // 获取 myAnnotation的值，并打印出来
                String[] values = myAnnotation.value();
                for (String str:values)
                    System.out.printf(str+", ");
                System.out.println();
            }

            // 获取方法上的所有注解，并打印出来
            Annotation[] annotations = method.getAnnotations();
            for(Annotation annotation : annotations){
                System.out.println(annotation);
            }
        }

    }
}


